package fetch.tilabdbplaces;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Set;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;


import data.Business;

public class ParserJson2Biz {
	
	public ParserJson2Biz(){
		
	}
	
	public void json2biz(File json, Set<Business> biz) {
		try {
			BufferedReader br = new BufferedReader(new FileReader(json));
			JsonElement jse = new JsonParser().parse(br);
			
			JsonArray jsa = jse.getAsJsonObject().getAsJsonArray("search_results");
		
			for (int i= 0; i<jsa.size(); i++ ) {
				JsonElement el = jsa.get(i);
				
				String name = checkNull(el.getAsJsonObject().get("name"));
				String address = checkNull(el.getAsJsonObject().get("street"));
				String category = checkNull(el.getAsJsonObject().get("category"));
				String city = checkNull(el.getAsJsonObject().get("city"));
				String subdivision = checkNull(el.getAsJsonObject().get("subdivision"));
				int sizeLevel = Integer.parseInt(checkNull(el.getAsJsonObject().get("sizeLevel")));
				int activityLevel = Integer.parseInt(checkNull(el.getAsJsonObject().get("activityLevel")));
				int checkins = Integer.parseInt(checkNull(el.getAsJsonObject().get("checkins")));
				double lat = Double.parseDouble(checkNull(el.getAsJsonObject().get("latitude")));
				double lon = Double.parseDouble(checkNull(el.getAsJsonObject().get("longitude")));
				
				Business b = new Business(name,address,category,city,subdivision,sizeLevel,activityLevel,checkins,lat,lon);
				biz.add(b);

		    }
					
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public String checkNull(JsonElement element){
		if(element == null)
			return "-1";
		else
			return element.getAsString();
	}

}


